//
//  YXLoginViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXLoginViewController.h"
#import "YXForgetPasswordViewController.h"
#import "YXRegistViewController.h"
#import "YXClientViewController.h"
#import "YXMarketViewController.h"
#import "YXTeamViewController.h"
#import "YXExchangeViewController.h"
#import "YXMyInfoViewController.h"

@interface YXLoginViewController ()

@end

@implementation YXLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    YXLoginView * loginView = [[YXLoginView alloc] init];
    self.view = loginView;
    _loginView = loginView;
    [loginView.numberText addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
    [loginView.passwordText addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
    [loginView.loginBtn addTarget:self action:@selector(touchLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    [loginView.forgetBtn addTarget:self action:@selector(touchForgetBtn) forControlEvents:UIControlEventTouchUpInside];
    [loginView.registerBtn addTarget:self action:@selector(touchRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"checkUpdate",@"type",@"2.4",@"version", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultcode = [dic objectForKey:@"resultCode"];
        if ([resultcode isEqualToString:@"0"]) {
            if ([[dic objectForKey:@"needUpdate"] isEqualToString:@"1"]) {
                
            }else{
                YXWindowInfo * windowInfo = [YXWindowInfo sharedWindowInfo];
                NSString * userAccount = [[NSUserDefaults standardUserDefaults] stringForKey:[windowInfo userAccount]];
                NSString * userPassword = [[NSUserDefaults standardUserDefaults] stringForKey:[windowInfo userPassword]];
                NSLog(@"%@  %@",userAccount,userPassword);
                if (userAccount && userPassword) {
                    [self sendLoginRequestWithAccount:userAccount Password:userPassword isSave:NO];
                }
            }
        }
    }];
}

- (void)showMainInterface
{
    YXClientViewController * clientVC = [[YXClientViewController alloc] init];
    clientVC.title = @"客户网";
    UINavigationController * clientNav = [[UINavigationController alloc] initWithRootViewController:clientVC];
    
    YXMarketViewController * marketVC = [[YXMarketViewController alloc] init];
    marketVC.title = @"销售";
    UINavigationController * marketNav = [[UINavigationController alloc] initWithRootViewController:marketVC];
    
    YXTeamViewController * teamVC = [[YXTeamViewController alloc] init];
    teamVC.title = @"团队";
    UINavigationController * teamNav = [[UINavigationController alloc] initWithRootViewController:teamVC];
    
    YXExchangeViewController * exchangeVC = [[YXExchangeViewController alloc] init];
    exchangeVC.title = @"交流";
    UINavigationController * exchangeNav = [[UINavigationController alloc] initWithRootViewController:exchangeVC];
    
    YXMyInfoViewController * setVC = [[YXMyInfoViewController alloc] init];
    setVC.title = @"我";
    UINavigationController * setNav = [[UINavigationController alloc] initWithRootViewController:setVC];
    
    UITabBarController * mainTabBar = [[UITabBarController alloc] init];
    [mainTabBar setViewControllers:@[clientNav,marketNav,teamNav,exchangeNav,setNav] animated:YES];
    UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    window.rootViewController = mainTabBar;
}

- (void)sendLoginRequestWithAccount:(NSString *)account Password:(NSString *)password isSave:(BOOL)isSave
{
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"login",@"type",@"2.4",@"version",account,@"phone",password,@"passWord", nil] dataBlock:^(NSDictionary *dic) {
        NSLog(@"%@",dic);
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            YXWindowInfo * windowInfo = [YXWindowInfo sharedWindowInfo];
            windowInfo.userInfoDic = dic;
            if (isSave) {
                [[NSUserDefaults standardUserDefaults] setObject:_loginView.numberText.text forKey:windowInfo.userAccount];
                [[NSUserDefaults standardUserDefaults] setObject:_loginView.passwordText.text forKey:windowInfo.userPassword];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
            [self showMainInterface];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

#pragma mark - ButtonsClick
- (void)keyboardDown
{
    
}

- (void)touchLoginBtn
{
    [self showMainInterface];
//    if ([_loginView.numberText.text isEqualToString:@""]) {
//        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入账号。"];
//        return;
//    }
//    if ([_loginView.passwordText.text isEqualToString:@""]) {
//        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入密码。"];
//        return;
//    }
//    [self sendLoginRequestWithAccount:_loginView.numberText.text Password:_loginView.passwordText.text isSave:YES];
}

- (void)touchForgetBtn
{
    YXForgetPasswordViewController * forgetPasswordVC = [[YXForgetPasswordViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:forgetPasswordVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)touchRegisterBtn
{
    YXRegistViewController * registVC = [[YXRegistViewController alloc] init];
    [self presentViewController:registVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
